package com.stlm2.dems.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.User;
import com.stlm2.util.Memcached;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.DdmObligation;
import com.stlm2.dems.dao.DdmObligationDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service("ddmObligationService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class DdmObligationService extends BaseServiceImpl<DdmObligation> {

	@Resource
	private DdmObligationDao ddmObligationDao;
	@Resource
	private UserDao userDao;
	@Resource
	private Memcached memcached;

	@Override
	protected BaseDao <DdmObligation> getDao() {
		return ddmObligationDao;
	}
	/*
	*查询服务承诺名称
	* zlq
	 */
	public String getDdmObligation(ModelMap modelMap){
		DdmObligation ddmObligation=new DdmObligation();
		List<DdmObligation> ddmObligationList=ddmObligationDao.queryPageView(-1,-1,ddmObligation).getRecords();

		return CodeContent.getModelByCode(1,"success", convertList(ddmObligationList));
	}
	public List<ModelMap> convertList(List<DdmObligation> ddmObligationList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (ddmObligationList != null && ddmObligationList.size() > 0) {
			for (DdmObligation ddmObligation : ddmObligationList) {
				ModelMap map = buildModelList(ddmObligation);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelList(DdmObligation ddmObligation) {
		ModelMap model = new ModelMap();
		model.put("id",ddmObligation.getId());
		model.put("name",ddmObligation.getName());
		return  model;
	}
	/*
 *添加服务承诺
 * zlq
 * token:
 * name:名称
 * label：标签
 * detailsUrl：详情路径
  */
	public  String add(ModelMap model, String token,String name,String label,String imgUrl,String details){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if(StringUtils.isEmpty(name)){
			return CodeContent.getModelByCode(0, "服务名称不能为空", model);
		}
		if(StringUtils.isEmpty(label)){
			return CodeContent.getModelByCode(0, "标签名不能为空", model);
		}
		if(StringUtils.isEmpty(imgUrl)){
			return CodeContent.getModelByCode(0, "图片路径不能为空", model);
		}
		if(StringUtils.isEmpty(details)){
			return CodeContent.getModelByCode(0, "服务详情不能为空", model);
		}
		//防止重复提交
		Object o = memcached.get(loginUser.getId()+"ADDDdmObligation");
		if(o!=null){
			return CodeContent.getModelByCode(0, "您操作频繁，请在5秒后重试", model);
		}else {
			memcached.set(loginUser.getId()+"ADDDdmObligation",5,"1");
		}
		DdmObligation ddmObligation=ddmObligationDao.getByLabel(label.trim());
		if(ddmObligation!=null){
			return CodeContent.getModelByCode(0, "标签名已存在", model);
		}else {
			ddmObligation=new DdmObligation();
		}
		ddmObligation.setLabel(label.trim());
		ddmObligation.setImgUrl(imgUrl);
		ddmObligation.setName(name);
		ddmObligation.setDetails(details);
		ddmObligationDao.add(ddmObligation);
		return CodeContent.getModelByCode(1, "", model);
	}

	/*
*CRM查询服务承诺列表
* zlq
* token:
* page:页码
 */
	public String getList(ModelMap model, String token,String page){
		PageView pageview = new PageView();
		int pageNo = 1;
		int pageSize = 20;
		if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
			pageNo = NumberUtils.toInt(page);
		}
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		DdmObligation ddmObligation=new DdmObligation();
		pageview=ddmObligationDao.queryPageView(pageNo, pageSize,ddmObligation);
		model.put("code",1);
		model.put("msg","success");
		if (pageview != null){
			model.put("datas",convertCrmList(pageview.getRecords()));
			model.put("recordsTotal", pageview.getRowCount());
			model.put("totalPage", pageview.getPageCount());
			model.put("pageNow", pageview.getPageNow());
		}
		return new JSONObject(model).toString();

	}
	public List<ModelMap> convertCrmList(List<DdmObligation> ddmObligationList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (ddmObligationList != null && ddmObligationList.size() > 0) {
			for (DdmObligation ddmObligation : ddmObligationList) {
				ModelMap map = buildModelCrmList(ddmObligation);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelCrmList(DdmObligation ddmObligation) {
		ModelMap model = new ModelMap();
		model.put("id",ddmObligation.getId());
		model.put("name",ddmObligation.getName());
		model.put("label",ddmObligation.getLabel());
		model.put("imgUrl",ddmObligation.getImgUrl());
		model.put("details",ddmObligation.getDetails());
		return  model;
	}
}